package service.impl;

import BBS.bean.PageInfo;
import BBS.bean.PostInfo;
import mapper.PageMapper;
import mapper.PostMapper;
import mapper.UserMapper;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import service.PostService;

import javax.servlet.ServletContext;
import java.util.List;

public class PostServiceImpl implements PostService {
    private PostMapper pm;
    private SqlSession session;
    private UserMapper um;

    public PostServiceImpl(ServletContext application) {
        SqlSessionFactory ssf = (SqlSessionFactory) application.getAttribute("SqlSessionFactory");
        session = ssf.openSession();
        pm = session.getMapper(PostMapper.class);
        um = session.getMapper(UserMapper.class);
    }

    @Override
    public List<PostInfo> findPostByPageId(Integer pageId) {
        List<PostInfo> list = pm.findPostByPageId(pageId);

        for (PostInfo pi:list) {
            pi.setPostUser(um.findUserByUserId(pi.getUserId()));
        }
        session.close();
        return list;
    }

}
